home *** CD-ROM | disk | FTP | other *** search
-
- LINK(5) UNIX Programmer's Manual LINK(5)
-
- NNAAMMEE
- lliinnkk - dynamic loader and link editor interface
-
- SSYYNNOOPPSSIISS
- ##iinncclluuddee <<lliinnkk..hh>>
-
- DDEESSCCRRIIPPTTIIOONN
- The include file <_l_i_n_k_._h> declares several structures that are present in
- dynamically linked programs and libraries. The structures define the in-
- terface between several components of the link-editor and loader mecha-
- nism. The layout of a number of these structures within the binaries re-
- sembles the a.out format in many places as it serves such similar func-
- tions as symbol definitions (including the accompanying string table) and
- relocation records needed to resolve references to external entities. It
- also records a number of data structures unique to the dynamic loading
- and linking process. These include references to other objects that are
- required to complete the link-editing process and indirection tables to
- facilitate _P_o_s_i_t_i_o_n _I_n_d_e_p_e_n_d_e_n_t _C_o_d_e (PIC for short) to improve sharing
- of code pages among different processes. The collection of data struc-
- tures described here will be refered to as the _R_u_n_-_t_i_m_e _R_e_l_o_c_a_t_i_o_n
- _S_e_c_t_i_o_n _(_R_R_S_) and is embedded in the standard text and data segments of
- the dynamically linked program or shared object image as the existing
- a.out format offers no room for it elsewhere.
-
- Several utilities co-operate to ensure that the task of getting a program
- ready to run can complete successfully in a way that optimizes the use of
- system resources. The compiler emits PIC code from which shared libraries
- can be built by ld(1). The compiler also includes size information of
- any initialized data items through the .size assembler directive. PIC
- code differs from conventional code in that it accesses data variables
- through an indirection table, the Global Offset Table, by convention ac-
- cessable by the reserved name ___G_L_O_B_A_L___O_F_F_S_E_T___T_A_B_L_E__. The exact mechanism
- used for this is machine dependent, usually a machine register is re-
- served for the purpose. The rational behind this construct is to generate
- code that is independent of the actual load address. Only the values con-
- tained in the Global Offset Table may need updating at run-time depending
- on the load addresses of the various shared objects in the address space.
-
- Likewise, procedure calls to globally defined functions are redirected
- through the Procedure Linkage Table (PLT) residing in the data segment of
- the core image. Again, this is done to avoid run-time modifications to
- the text segment.
-
- The linker-editor allocates the Global Offset Table and Procedure Linkage
- Table when combining PIC object files into an image suitable for mapping
- into the process address space. It also collects all symbols that may be
- needed by the run-time link-editor and stores these along with the im-
- age's text and data bits. Another reserved symbol, ___D_Y_N_A_M_I_C is used to
- indicate the presence of the run-time linker structures. Whenever _DYNAM-
- IC is relocated to 0, there is no need to invoke the run-time link-edi-
- tor. If this symbol is non-zero, it points at a data structure from which
- the location of the necessary relocation- and symbol information can be
- derived. This is most notably used by the start-up module, _c_r_t_0. The _DY-
- NAMIC structure is conventionally located at the start of the data seg-
- ment of the image to which it pertains.
-
- DDAATTAA SSTTRRUUCCTTUURREESS
- The data structures supporting dynamic linking and run-time relocation
- reside both in the text and data segments of the image they apply to.
- The text segments contain read-only data such as symbols descriptions and
- names, while the data segments contain the tables that need to be modi-
- fied by during the relocation process.
-
- The _DYNAMIC symbol references a ___d_y_n_a_m_i_c structure:
-
- struct _dynamic {
- int d_version;
- struct so_debug *d_debug;
- union {
- struct section_dispatch_table *d_sdt;
- } d_un;
- struct ld_entry *d_entry;
- };
-
- _d___v_e_r_s_i_o_n This field provides for different versions of the dynamic
- linking implementation. The current version numbers understood
- by ld and ld.so are _L_D___V_E_R_S_I_O_N___S_U_N _(_3_)_, which is used by the
- SunOS 4.x releases, and _L_D___V_E_R_S_I_O_N___B_S_D _(_8_)_, which is currently
- in use by NetBSD.
-
- _d___u_n Refers to a _d___v_e_r_s_i_o_n dependent data structure.
-
- _d___d_e_b_u_g this field provides debuggers with a hook to access symbol ta-
- bles of shared objects loaded as a result of the actions of
- the run-time link-editor.
-
- _d___e_n_t_r_y this field is obsoleted by CRT interface version CRT_VER-
- SION_BSD4, and is replaced by the crt_ldentry in _c_r_t___l_d_s_o.
-
- The _s_e_c_t_i_o_n___d_i_s_p_a_t_c_h___t_a_b_l_e structure is the main ``dispatcher'' table,
- containing offsets into the image's segments where various symbol and re-
- location information is located.
-
- struct section_dispatch_table {
- struct so_map *sdt_loaded;
- long sdt_sods;
- long sdt_paths;
- long sdt_got;
- long sdt_plt;
- long sdt_rel;
- long sdt_hash;
- long sdt_nzlist;
- long sdt_filler2;
- long sdt_buckets;
- long sdt_strings;
- long sdt_str_sz;
- long sdt_text_sz;
- long sdt_plt_sz;
- };
-
- _s_d_t___l_o_a_d_e_d A pointer to the first link map loaded (see below). This
- field is set by ld.so for the benefit of debuggers that may
- use it to load a shared object's symbol table.
-
- _s_d_t___s_o_d_s The start of a (linked) list of shared object descriptors
- needed by _t_h_i_s object.
-
- _s_d_t___p_a_t_h_s Library search rules. A colon separated list of directories
- corresponding to the --RR option of ld(1).
-
- _s_d_t___g_o_t The location of the Global Offset Table within this image.
-
- _s_d_t___p_l_t The location of the Procedure Linkage Table within this im-
- age.
-
- _s_d_t___r_e_l The location of an array of _r_e_l_o_c_a_t_i_o_n___i_n_f_o structures (see
-
-
- a.out(5)) specifying run-time relocations.
-
- _s_d_t___h_a_s_h The location of the hash table for fast symbol lookup in this
- object's symbol table.
-
- _s_d_t___n_z_l_i_s_t The location of the symbol table.
-
- _s_d_t___f_i_l_l_e_r_2
- Currently unused.
-
- _s_d_t___b_u_c_k_e_t_s
- The number of buckets in _s_d_t___h_a_s_h
-
- _s_d_t___s_t_r_i_n_g_s
- The location of the symbol string table that goes with
- _s_d_t___n_z_l_i_s_t.
-
- _s_d_t___s_t_r___s_z The size of the string table.
-
- _s_d_t___t_e_x_t___s_z
- The size of the object's text segment.
-
- _s_d_t___p_l_t___s_z The size of the Procedure Linkage Table.
-
- A _s_o_d structure descibes a shared object that is needed to complete the
- link edit process of the object containing it. A list of such objects
- (chained through _s_o_d___n_e_x_t) is pointed at by the _s_d_t___s_o_d_s in the sec-
- tion_dispatch_table structure.
-
- struct sod {
- long sod_name;
- u_int sod_library : 1,
- sod_unused : 31;
- short sod_major;
- short sod_minor;
- long sod_next;
- };
-
- _s_o_d___n_a_m_e The offset in the text segment of a string describing this
- link object.
-
- _s_o_d___l_i_b_r_a_r_y If set, _s_o_d___n_a_m_e specifies a library that is to be searched
- for by ld.so. The path name is obtained by searching a set
- of directories (see also ldconfig(8)) for a shared object
- matching _l_i_b_<_s_o_d___n_a_m_e_>_._s_o_._n_._m. If not set, _s_o_d___n_a_m_e should
- point at a full path name for the desired shared object.
-
- _s_o_d___m_a_j_o_r Specifies the major version number of the shared object to
- load.
-
- _s_o_d___m_i_n_o_r Specifies the prefered minor version number of the shared
- object to load.
-
- The run-time link-editor maintains a list of structures called _l_i_n_k _m_a_p_s
- to keep track of all shared objects loaded into a process' address space.
- These structures are only used at run-time and do not occur within the
- text or data segment of an executable or shared library.
-
- struct so_map {
- caddr_t som_addr;
- char *som_path;
- struct so_map *som_next;
- struct sod *som_sod;
- caddr_t som_sodbase;
- u_int som_write : 1;
- struct _dynamic *som_dynamic;
- caddr_t som_spd;
- };
-
- _s_o_m___a_d_d_r The address at which the shared object associated with this
- link map has been loaded.
-
- _s_o_m___p_a_t_h The full path name of the loaded object.
-
- _s_o_m___n_e_x_t Pointer to the next link map.
-
- _s_o_m___s_o_d The _s_o_d structure that was responsible for loading this
- shared object.
-
- _s_o_m___s_o_d_b_a_s_e Tossed in later versions the run-time linker.
-
- _s_o_m___w_r_i_t_e Set if (some portion of) this object's text segment is cur-
- rently writable.
-
- _s_o_m___d_y_n_a_m_i_c Pointer to this object's ___d_y_n_a_m_i_c structure.
-
- _s_o_m___s_p_d Hook for attaching private data maintained by the run-time
- link-editor.
-
- Symbol description with size. This is simply an _n_l_i_s_t structure with one
- field (_n_z___s_i_z_e) added. Used to convey size information on items in the
- data segment of shared objects. An array of these lives in the shared ob-
- ject's text segment and is addressed by the _s_d_t___n_z_l_i_s_t field of
- _s_e_c_t_i_o_n___d_i_s_p_a_t_c_h___t_a_b_l_e.
-
- struct nzlist {
- struct nlist nlist;
- u_long nz_size;
- #define nz_un nlist.n_un
- #define nz_strx nlist.n_un.n_strx
- #define nz_name nlist.n_un.n_name
- #define nz_type nlist.n_type
- #define nz_value nlist.n_value
- #define nz_desc nlist.n_desc
- #define nz_other nlist.n_other
- };
-
- _n_l_i_s_t (see nlist(5)).
-
- _n_z___s_i_z_e The size of the data represented by this symbol.
-
- A hash table is included within the text segment of shared object to to
- facilitate quick lookup of symbols during run-time link-editing. The
- _s_d_t___h_a_s_h field of the _s_e_c_t_i_o_n___d_i_s_p_a_t_c_h___t_a_b_l_e structure points at an array
- of _r_r_s___h_a_s_h structures:
-
- struct rrs_hash {
- int rh_symbolnum; /* symbol number */
- int rh_next; /* next hash entry */
- };
-
- _r_h___s_y_m_b_o_l_n_u_m The index of the symbol in the shared object's symbol table
- (as given by the _l_d___s_y_m_b_o_l_s field).
-
- _r_h___n_e_x_t In case of collisions, this field is the offset of the next
- entry in this hash table bucket. It is zero for the last
- bucket element.
- The _r_t___s_y_m_b_o_l structure is used to keep track of run-time allocated com-
- mons and data items copied from shared objects. These items are kept on
- linked list and is exported through the _d_d___c_c field in the _s_o___d_e_b_u_g
- structure (see below) for use by debuggers.
-
- struct rt_symbol {
- struct nzlist *rt_sp;
- struct rt_symbol *rt_next;
- struct rt_symbol *rt_link;
- caddr_t rt_srcaddr;
- struct so_map *rt_smp;
- };
-
- _r_t___s_p The symbol description.
-
- _r_t___n_e_x_t Virtual address of next rt_symbol.
-
- _r_t___l_i_n_k Next in hash bucket. Used by internally by ld.so.
-
- _r_t___s_r_c_a_d_d_r Location of the source of initialized data within a shared
- object.
-
- _r_t___s_m_p The shared object which is the original source of the data
- that this run-time symbol describes.
-
- The _s_o___d_e_b_u_g structure is used by debuggers to gain knowledge of any
- shared objects that have been loaded in the process's address space as a
- result of run-time link-editing. Since the run-time link-editor runs as a
- part of process initialization, a debugger that wishes to access symbols
- from shared objects can only do so after the link-editor has been called
- from crt0. A dynamically linked binary contains a _s_o___d_e_b_u_g structure
- which can be located by means of the _d___d_e_b_u_g field in ___d_y_n_a_m_i_c.
-
- struct so_debug {
- int dd_version;
- int dd_in_debugger;
- int dd_sym_loaded;
- char *dd_bpt_addr;
- int dd_bpt_shadow;
- struct rt_symbol *dd_cc;
- };
-
- _d_d___v_e_r_s_i_o_n Version number of this interface.
-
- _d_d___i_n___d_e_b_u_g_g_e_r Set by the debugger to indicate to the run-time linker
- that the program is run under control of a debugger.
-
- _d_d___s_y_m___l_o_a_d_e_d Set by the run-time linker whenever it adds symbols by
- loading shared objects.
-
- _d_d___b_p_t___a_d_d_r The address were a breakpoint will be set by the the run-
- time linker to divert control to the debugger. This ad-
- dress is determined by the start-up module, _c_r_t_0_._o_, to be
- some convenient place before the call to _main.
-
- _d_d___b_p_t___s_h_a_d_o_w Contains the original instruction that was at
- _d_d___b_p_t___a_d_d_r. The debugger is expected to put this in-
- struction back before continuing the program.
-
- _d_d___c_c A pointer to the linked list of run-time allocated sym-
- bols that the debugger may be interested in.
-
- The _l_d___e_n_t_r_y structure defines a set of service routines within ld.so.
- See dlfcn(3) for more information.
-
- struct ld_entry {
- void *(*dlopen)(char *, int);
- int (*dlclose)(void *);
- void *(*dlsym)(void *, char *);
- int (*dlctl)(void *, int, void *);
- void (*dlexit) __P((void));
- };
-
- The _c_r_t___l_d_s_o structure defines the interface between ld.so and the start-
- up code in crt0.
-
- struct crt_ldso {
- int crt_ba;
- int crt_dzfd;
- int crt_ldfd;
- struct _dynamic *crt_dp;
- char **crt_ep;
- caddr_t crt_bp;
- char *crt_prog;
- char *crt_ldso;
- char *crt_ldentry;
- };
- #define CRT_VERSION_SUN 1
- #define CRT_VERSION_BSD2 2
- #define CRT_VERSION_BSD3 3
- #define CRT_VERSION_BSD4 4
-
- _c_r_t___b_a The virtual address at which ld.so was loaded by crt0.
-
- _c_r_t___d_z_f_d On SunOS systems, this field contains an open file descriptor
- to ``/dev/zero'' used to get demand paged zeroed pages. On
- NetBSD systems it contains -1.
-
- _c_r_t___l_d_f_d Contains an open file descriptor that was used by crt0 to load
- ld.so.
-
- _c_r_t___d_p A pointer to main's ___d_y_n_a_m_i_c structure.
-
- _c_r_t___e_p A pointer to the environment strings.
-
- _c_r_t___b_p The address at which a breakpoint will be placed by the run-
- time linker if the main program is run by a debugger. See
- _s_o___d_e_b_u_g
-
- _c_r_t___p_r_o_g The name of the main program as determined by crt0 (CRT_VER-
- SION_BSD3 only).
-
- _c_r_t___l_d_s_o The path of the run-time linker as mapped by crt0 (CRT_VER-
- SION_BSD4 only).
-
- _c_r_t___l_d_e_n_t_r_y
- The dlfcn(3) entry points provided by the run-time linker
- (CRT_VERSION_BSD4 only).
-
- The _h_i_n_t_s___h_e_a_d_e_r and _h_i_n_t_s___b_u_c_k_e_t structures define the layout of the li-
- brary hints, normally found in ``/var/run/ld.so.hints,'' which is used by
- ld.so to quickly locate the shared object images in the filesystem. The
- organization of the hints file is not unlike that of an ``a.out'' object
- file, in that it contains a header determining the offset and size of a
- table of fixed sized hash buckets and a common string pool.
-
- struct hints_header {
- long hh_magic;
- #define HH_MAGIC 011421044151
- long hh_version;
- #define LD_HINTS_VERSION_1 1
- long hh_hashtab;
- long hh_nbucket;
- long hh_strtab;
- long hh_strtab_sz;
- long hh_ehints;
- };
-
-
- _h_h___m_a_g_i_c Hints file magic number.
-
- _h_h___v_e_r_s_i_o_n Interface version number.
-
- _h_h___h_a_s_h_t_a_b Offset of hash table.
-
- _h_h___s_t_r_t_a_b Offset of string table.
-
- _h_h___s_t_r_t_a_b___s_z Size of strings.
-
- _h_h___e_h_i_n_t_s Maximum usable offset in hints file.
-
- /*
- * Hash table element in hints file.
- */
- struct hints_bucket {
- int hi_namex;
- int hi_pathx;
- int hi_dewey[MAXDEWEY];
- int hi_ndewey;
- #define hi_major hi_dewey[0]
- #define hi_minor hi_dewey[1]
- int hi_next;
- };
-
- _h_i___n_a_m_e_x Index of the string identifying the library.
-
- _h_i___p_a_t_h_x Index of the string representing the full path name of the li-
- brary.
-
- _h_i___d_e_w_e_y The version numbers of the shared library.
-
- _h_i___n_d_e_w_e_y The number of valid entries in _h_i___d_e_w_e_y.
-
- _h_i___n_e_x_t Next bucket in case of hashing collisions.
-
-
- CCAAVVEEAATTSS
- Only the (GNU) C compiler currently supports the creation of shared li-
- braries. Other programming languages can not be used.
-
-
- BSD Experimental October 23, 1993 7
-